﻿Imports System.Globalization
Partial Class SendData
    Inherits System.Web.UI.Page
    Private mts As Data.SqlClient.SqlTransaction
    Private curcon As Data.SqlClient.SqlConnection
    Private Function Connect(ByRef Con As System.Data.SqlClient.SqlConnection, ByRef ts As System.Data.SqlClient.SqlTransaction) As Integer


        Con.ConnectionString = ConfigurationManager.ConnectionStrings("[MyConn]").ToString()
        tryClose()
        Try

            Con.Open()
            curcon = Con
            ts = Con.BeginTransaction(Data.IsolationLevel.Serializable)
            mts = ts
            Return 1
        Catch
            Return 0

        End Try

    End Function
    Private Function CloseMe(ByRef ts As System.Data.SqlClient.SqlTransaction, ByRef Con As System.Data.SqlClient.SqlConnection) As Integer
        ts.Commit()
        mts = Nothing
        Con.Close()
        curcon = Nothing
        Return 0
    End Function
    Private Function ChangeField(ByRef Cn As System.Data.SqlClient.SqlConnection, ByVal Value As Object, ByVal UserID As String, ByVal FieldName As String) As Integer

        Dim cmd As New System.Data.SqlClient.SqlCommand

        cmd.Connection = Cn
        cmd.Transaction = mts

        Try

            cmd.Parameters.AddWithValue("UsrName", UserID)
            cmd.Parameters.AddWithValue("Valued", Value)
            cmd.CommandText = "UPDATE Main Set LastWriteDate= GetDate(), " + FieldName.ToString() + " = @Valued Where ID= @UsrName"

            cmd.ExecuteNonQuery()
            Return 1
        Catch
            Return 0

        End Try
        Return 0
    End Function
    Private Function GetFile(ByVal cn As Data.SqlClient.SqlConnection, ByVal Id As String, ByVal FileId As String, ByRef OutInfo As String) As Integer


        Dim cmd As New Data.SqlClient.SqlCommand

        cmd.Parameters.AddWithValue("FileId", FileId)
        cmd.Parameters.AddWithValue("UserId", Id)

        cmd.CommandText = "SELECT DATA from FILES WHERE FileID=@FileId AND UserId=@UserId Order by FilePart ASC"

        Dim dr As System.Data.SqlClient.SqlDataReader
        dr = GetReader(cn, cmd)
        If dr Is Nothing Then
            Return 0
        End If
        Dim r As New System.Text.StringBuilder

        If dr.HasRows Then
            r.Append(dr.GetValue(0).ToString.Trim)
        End If
        While dr.Read()
            r.Append(dr.GetValue(0).ToString.Trim)
        End While
        dr.Close()
        OutInfo = r.ToString()
        Return 1

    End Function
    Private Function CheckBFileInsert(ByRef cn As Data.SqlClient.SqlConnection, ByRef Id As String, ByRef InFileId As String, Optional ByVal FirstPart As Boolean = False) As Integer


        Dim cmd As New System.Data.SqlClient.SqlCommand

        cmd.Connection = cn
        cmd.Transaction = mts

        Try

            cmd.Parameters.AddWithValue("a1", Id)
            cmd.Parameters.AddWithValue("a2", InFileId)
            cmd.CommandText = "SELECT COUNT(fileid) From FILES  Where FileId=@a2 and UserId=@a1 "

            Dim ca As Long
            ca = cmd.ExecuteScalar()
            If ca = 0 Then Return 1 Else If Not FirstPart Then Return 0




        Catch
            Return 0

        End Try

        If FirstPart Then
            Try
                cmd.Parameters.Remove(cmd.Parameters.Item(0))
                cmd.Parameters.Remove(cmd.Parameters.Item(0))
                cmd.Parameters.AddWithValue("a1", Id)
                cmd.Parameters.AddWithValue("a2", InFileId)
                cmd.CommandText = "DELETE From FILES  Where FileId=@a2 AND UserId=@a1 "

                If cmd.ExecuteNonQuery() = -1 Then
                    Return 0
                Else
                    Return 1
                End If




            Catch
                Return 0

            End Try

        End If





        Return 0



    End Function

    Private Function InsertFilePart(ByVal cn As Data.SqlClient.SqlConnection, ByRef txt As String, ByRef part As Integer, ByRef Fid As String, ByRef UId As String) As Integer


        Dim cmd As New System.Data.SqlClient.SqlCommand

        cmd.Connection = cn
        cmd.Transaction = mts

        Try

            cmd.Parameters.AddWithValue("a1", Fid)
            cmd.Parameters.AddWithValue("a2", part)
            cmd.Parameters.AddWithValue("a3", UId)
            cmd.Parameters.AddWithValue("a4", txt)
            cmd.CommandText = "INSERT INTO FILES  (FileId,FilePart,UserId,DATA) VALUES (@a1,@a2,@a3,@a4) "

            cmd.ExecuteNonQuery()
            Return 1
        Catch
            Return 0

        End Try
        Return 0




    End Function
    Private Function Receive(ByVal txt As String, ByVal Id As String, ByVal Random As String, ByVal DateMax As DateTime, ByVal SignatureT As String, ByVal Part As Integer, ByVal FileId As String, ByVal withnocypher As Boolean) As Integer

        If Math.Abs(DateDiff("s", DateMax, Now)) > 10 Then Return 0


        Dim cn As New Data.SqlClient.SqlConnection
        Dim MySKey As String
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing
        If Connect(cn, ts) = 0 Then Return 0
        Dim oin As Integer, lvd As DateTime
        If GetInfo(cn, Id, oin, "Status", False) = 0 Then Return 0
        If oin <> 1 Then Return 0


        If GetInfo(cn, Id, lvd, "LASTWRITEDATE", False) = 0 Then Return 0
        Dim cbu1 As Long
        Dim mbu As Long
        If Me.GetInfo(cn, Id, mbu, "MaxBand", False) = 0 Then Return 0

        If Me.GetInfo(cn, Id, cbu1, "CurrentBandUsed", False) = 0 Then Return 0


        If cbu1 > mbu Then Return 0

        If ChangeStatus(cn, Id, 2) = 0 Then Return 0



        MySKey = "%NOTHING%"


        If Not withnocypher Then

            Dim dr As System.Data.SqlClient.SqlDataReader

            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cn
            cmd.Transaction = mts

            cmd.Parameters.AddWithValue("UsrName", Id)
            cmd.CommandText = "SELECT SKey From Main Where ID= @UsrName"
            dr = cmd.ExecuteReader()
            While dr.Read()
                MySKey = dr.GetString(0).ToString.Trim
            End While
            If MySKey = "%NOTHING%" Then Return 0
            dr.Close()


        End If
        Dim fieldname As String
        If Not withnocypher Then

            fieldname = "MyField"
        Else
            fieldname = "UnCypheredField"

        End If


        Dim sg As New Signature.SignatureClass
        Dim IsVerified As Boolean = False

        If sg.Code(Part.ToString() + "/&/" + MySKey.ToString() + "/&/" + txt.ToString() + "/&/" + Id.ToString() + "/&/" + Random.ToString() + "/&/" + DateMax.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture), 2, 3, 4, 5) = SignatureT.ToString() Then
            IsVerified = True


        End If
        If Not IsVerified Then

            Return 0
        End If

        If Part = -1 Then
            If Page.Request("IdUsr").Trim.ToUpper = "PUB" Then Return 0
            If CheckBFileInsert(cn, Id, FileId, True) = 0 Then Return 0
            GoTo ok
        Else
            If Part = 1 Then
                If CheckBFileInsert(cn, Id, FileId, False) = 0 Then Return 0

            End If
        End If
        If InsertFilePart(cn, txt, Part, FileId, Id) = 0 Then Return 0

        Dim cbu As Long
        If Me.GetInfo(cn, Id, cbu, "CurrentBandUsed", False) = 0 Then Return 0
        If Me.ChangeField(cn, cbu + txt.Length, Id, "CurrentBandUsed") = 0 Then Return 0

ok:
        If Me.ChangeField(cn, 1, Id, "Status") = 0 Then Return 0
        CloseMe(ts, cn)
        Return 1




    End Function
    Private Function GetReader(ByRef cn As Data.SqlClient.SqlConnection, ByRef cmd As Data.SqlClient.SqlCommand) As Data.SqlClient.SqlDataReader
        Dim dr As Data.SqlClient.SqlDataReader
        Try
            cmd.Connection = cn
            cmd.Transaction = mts
            dr = cmd.ExecuteReader()
            GetReader = Nothing
            While dr.Read
                GetReader = dr

                Exit Try
            End While
            dr.Close()
            dr = Nothing

        Catch
            GetReader = Nothing
        End Try

    End Function

    Private Function GetInfo(ByRef cn As Data.SqlClient.SqlConnection, ByRef Id As String, ByRef OutInfo As Object, ByRef fname As String, ByVal withnocyphered As Boolean) As Integer



        Dim fieldname As String


        fieldname = "MyField"
        If fname <> "" Then fieldname = fname
        If withnocyphered Then fieldname = "Uncypheredfield"



        Dim cmd As New Data.SqlClient.SqlCommand

        cmd.Parameters.AddWithValue("UsrName", Id)
        cmd.CommandText = "SELECT " + fieldname.ToString() + ",ID From Main Where ID=@UsrName"

        Dim dr As System.Data.SqlClient.SqlDataReader
        dr = GetReader(cn, cmd)
        If dr Is Nothing Then
            Return 0
        End If
        OutInfo = dr.GetValue(0)
        If TypeOf OutInfo Is String Then OutInfo = OutInfo.ToString.Trim
        Id = dr.GetValue(1).ToString.Trim
        dr.Close()
        Return 1


    End Function

    Private Function Look(ByRef cn As Data.SqlClient.SqlConnection, ByRef Id As String, ByRef Info As String, ByRef fname As String) As Integer



        Dim fieldname As String = ""


        If fname <> "" Then fieldname = fname



        Dim cmd As New Data.SqlClient.SqlCommand

        cmd.Parameters.AddWithValue("criteriaF", Info)
        cmd.CommandText = "SELECT " + fieldname.ToString() + ",ID From Main Where " + fieldname.ToString() + " like @criteriaF"

        Dim dr As System.Data.SqlClient.SqlDataReader
        dr = GetReader(cn, cmd)
        If dr Is Nothing Then
            Return 0
        End If
        Info = dr.GetValue(0)
        If TypeOf Info Is String Then Info = Info.ToString.Trim
        Id = dr.GetValue(1).ToString.Trim
        dr.Close()
        Return 1


    End Function


    Private Function CheckAccess(ByRef cn As Data.SqlClient.SqlConnection, ByVal Id1 As String, ByVal Id2 As String) As Integer
        Dim sList As String = ""




        If GetInfo(cn, Id1, sList, "FriendsIds", False) = 0 Then Return 0

        If sList.ToLower().Contains("*" + Id2.ToLower() + ",") Then
            CheckAccess = 1
            Exit Function

        End If



        CheckAccess = 0
    End Function
    Private Function GetFileDir(ByRef cn As Data.SqlClient.SqlConnection, ByRef Id As String, ByRef OutInfo As String) As Integer
        Dim cmd As New Data.SqlClient.SqlCommand

        cmd.Connection = cn
        cmd.Transaction = mts
        cmd.Parameters.AddWithValue("Id", Id)

        cmd.CommandText = "SELECT DISTINCT FILEID FROM FILES WHERE UserID=@id"
        Dim dr As System.Data.SqlClient.SqlDataReader
        dr = GetReader(cn, cmd)
        If dr Is Nothing Then
            Return 0
        End If

        Dim r As New System.Text.StringBuilder

        If dr.HasRows Then
            r.Append("*" + dr.GetValue(0).ToString.Trim + ",")

        End If
        While dr.Read()
            r.Append("*" + dr.GetValue(0).ToString.Trim + ",")

        End While
        dr.Close()
        OutInfo = r.ToString()
        Return 1



    End Function

    Private Function Dir(ByVal IdUsr As String, ByVal IdFrom As String, ByRef outinfo As String, ByVal MaxDate As DateTime, ByVal signature As String) As Integer

        Dim cn As New Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0

        If CheckCommandRequestSignature(cn, IdUsr, MaxDate, Signature) = 0 Then Return (0)
        If CheckAccess(cn, IdFrom, IdUsr) = 0 Then Return 0


        Dim cbu As Long
        If GetInfo(cn, IdFrom, cbu, "Currentbandused", False) = 0 Then Return 0
        Dim mbu As Long
        If GetInfo(cn, IdFrom, mbu, "MaxBand", False) = 0 Then Return 0
        If cbu > mbu Then Return 0

        If GetFileDir(cn, IdFrom, outinfo) = 0 Then Return 0


        CloseMe(ts, cn)
        Return 1

    End Function
    Private Function GetS(ByVal IdFrom As String, ByVal IdTo As String, ByVal FId As String, ByRef OutInfo As String, ByVal MaxDate As DateTime, ByVal signature As String) As Integer
        Dim cn As New Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0

        If CheckCommandRequestSignature(cn, IdTo, MaxDate, signature) = 0 Then Return (0)
        If CheckAccess(cn, IdFrom, IdTo) = 0 Then Return 0

        Dim oin As Integer
        If GetInfo(cn, IdFrom, oin, "Status", False) = 0 Then Return 0
        If oin <> 1 Then Return 0
        Dim lwd As DateTime
        If GetInfo(cn, IdFrom, lwd, "LastWriteDate", False) = 0 Then Return 0
        If Math.Abs(DateDiff(DateInterval.Second, lwd, Now)) < 15 Then Return 0


        Dim cbu As Long
        If GetInfo(cn, IdFrom, cbu, "Currentbandused", False) = 0 Then Return 0
        Dim mbu As Long
        If GetInfo(cn, IdFrom, mbu, "MaxBand", False) = 0 Then Return 0
        If cbu > mbu Then Return 0

        If GetFile(cn, IdFrom, FId, OutInfo) = 0 Then Return 0






        If ChangeField(cn, cbu + OutInfo.Length, IdTo, "Currentbandused") = 0 Then Return 0

        CloseMe(ts, cn)
        Return 1

    End Function

    Private Function GetDate() As DateTime
        GetDate = Now()
    End Function
    Private Function CheckCommandRequestSignature(ByRef cn As System.Data.SqlClient.SqlConnection, ByVal Id As String, ByVal MaxDate As DateTime, ByVal Signature As String, Optional ByVal withuncyphered As Boolean = False) As Integer
        Dim oi As String = ""

        If Not withuncyphered Then
            If GetInfo(cn, Id, oi, "SKey", False) = 0 Then Return 0
        Else
            oi = ""
        End If
        Dim sg As New Signature.SignatureClass
        If Math.Abs(DateDiff("s", MaxDate, Now)) > 10 Then Return 0
        If sg.Code(Id + "/&/" + MaxDate.ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture) + "/&/" + oi.ToString(), 2, 3, 4, 6) <> Signature Then Return 0
        Return 1


    End Function
    Private Function AddId(ByVal IdUsr As String, ByVal Id As String, ByVal FieldName As String, ByVal MaxDate As DateTime, ByVal Signature As String, Optional ByVal first As Boolean = False) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, IdUsr, MaxDate, Signature) = 0 Then Return 0

        Dim oi As String = ""
        If first Then If ChangeField(cn, "", IdUsr, FieldName) = 0 Then Return 0

        If GetInfo(cn, IdUsr, oi, FieldName, False) = 0 Then Return 0

        If Not oi.ToLower().Contains("*" + Id.ToLower() + ",") Then
            If ChangeField(cn, oi.ToString() + "*" + Id.ToString() + ",", IdUsr, FieldName) = 0 Then Return 0

        End If

        CloseMe(ts, cn)
        Return 1

    End Function
    Private Function GetU2(ByVal IdUsr As String, ByRef outinfo As String, ByVal MaxDate As DateTime, ByVal Signature As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, IdUsr, MaxDate, Signature) = 0 Then Return 0


        If GetInfo(cn, IdUsr, outinfo, "UnCypheredField2", False) = 0 Then Return 0

        CloseMe(ts, cn)
        Return 1

    End Function
    Private Function ChangeEmail(ByVal IdUsr As String, ByVal Email As String, ByVal MaxDate As DateTime, ByVal Signature As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, IdUsr, MaxDate, Signature) = 0 Then Return 0

        If ChangeField(cn, Email, IdUsr, "Email") = 0 Then Return 0

        CloseMe(ts, cn)
        Return 1

    End Function

    Private Function RemId(ByVal IdUsr As String, ByVal Id As String, ByVal FieldName As String, ByVal MaxDate As DateTime, ByVal Signature As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, IdUsr, MaxDate, Signature) = 0 Then Return (0)

        Dim oi As String = ""

        If GetInfo(cn, IdUsr, oi, FieldName, False) = 0 Then Return 0
        If oi.ToLower().Contains("*" + Id.ToLower() + ",") Then
            If ChangeField(cn, oi.ToLower().Replace("*" + Id.ToLower().ToString() + ",", ""), IdUsr, FieldName) = 0 Then Return 0

        End If
        CloseMe(ts, cn)
        Return 1

    End Function


    Private Function GetUnCypheredOnce(ByVal Id As String, ByRef outinfo As String) As Integer

        Dim cn As New Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing
        If Connect(cn, ts) = 0 Then Return 0



        If GetInfo(cn, Id, outinfo, "UncypheredField", True) = 0 Then Return 0
        If outinfo = "" Then Return 0
        If ChangeField(cn, "", Id, "UncypheredField") = 0 Then
            outinfo = ""
            Return 0
        End If


        CloseMe(ts, cn)
        Return 1


    End Function
    Private Function LookUpContactDetails(ByRef IdUsr As String, ByRef Id As String, ByRef NM As String, ByRef SName As String, ByRef Email As String, ByVal Md As DateTime, ByVal SN As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, IdUsr, Md, SN) = 0 Then Return 0

        Dim oi As String = ""
        Dim oi2 As String = ""
        Dim oi3 As String = ""
        Dim oi4 As String = ""


        Dim id2 As String

        id2 = Id
        GetInfo(cn, id2, oi, "ID", False)

        GetInfo(cn, id2, oi4, "Email", False)

        GetInfo(cn, id2, oi3, "SurName", False)

        GetInfo(cn, id2, oi2, "Name", False)





        CloseMe(ts, cn)
        Id = oi
        NM = oi2
        SName = oi3
        Email = oi4
        Return 1


    End Function

    Private Function LookDetails(ByRef Id As String, ByRef Nm As String, ByRef SName As String, ByRef Email As String, ByVal Md As DateTime, ByVal SN As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, Id, Md, SN) = 0 Then Return 0

        Dim oi As String = ""
        Dim oi2 As String = ""
        Dim oi3 As String = ""
        Dim oi4 As String = ""


        Dim id2 As String
        id2 = Id

        Dim id3 As String
        id3 = ""
        If Email.ToUpper.Trim = "NOTHING" Or Email.Trim = "" Then Email = "r3 aw893QC UL IAF CLKUHAJF, ,KAJ,MJ F.KZ,S"

        If Nm.ToUpper.Trim = "NOTHING" Or Nm.ToUpper.Trim = "" Then Nm = "WQJCUYCHNI3U KRCNUKX WBNIAEYUCYDSFYA<X<DSBMNSHM A GHA EKegfaag gsg szd"
        If SName.ToUpper.Trim = "NOTHING" Or SName.Trim = "" Then SName = "ewrr QW HOUIRQWUOIRQWJ09R32JJILIEWRAr wrqe"

        Look(cn, id3, Email, "Email")
        If id3 = "" Then

            Look(cn, id3, Nm.Trim.ToUpper() + " " + SName.Trim.ToUpper(), " UPPER(Name +' '+ SurName)")
            If id3 = "" Then Return 0
        End If
        Id = id3
        id2 = Id
        'regresa al que encontró
        'para obtener detalles propios use nothing en los campos email,sname y nm
        GetInfo(cn, id2, oi, "ID", False)

        GetInfo(cn, id2, oi4, "Email", False)

        GetInfo(cn, id2, oi3, "SurName", False)

        GetInfo(cn, id2, oi2, "Name", False)





        CloseMe(ts, cn)
        Id = oi
        Nm = oi2
        SName = oi3
        Email = oi4
        Return 1


    End Function
    Private Function GetIds(ByVal idusr As String, ByVal fieldname As String, ByRef outinfo As String, ByVal md As DateTime, ByVal sn As String) As Integer
        Dim cn As New System.Data.SqlClient.SqlConnection
        Dim ts As System.Data.SqlClient.SqlTransaction = Nothing

        If Connect(cn, ts) = 0 Then Return 0
        If CheckCommandRequestSignature(cn, idusr, md, sn) = 0 Then Return 0


        If GetInfo(cn, idusr, outinfo, fieldname, False) = 0 Then Return 0

        CloseMe(ts, cn)
        Return 1



    End Function



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim ouinfo As String = ""

            If Math.Abs(Now.TimeOfDay.TotalMilliseconds - ModuleA.LastVisit.TimeOfDay.TotalMilliseconds) < 10 Then GoTo fail

            If Not Page.Request("MD") Is Nothing Then If Math.Abs(DateDiff(DateInterval.Second, CDate(Page.Request("MD")), Now)) > 3 Then GoTo fail
            ModuleA.LastVisit = Now

            Select Case Page.Request("Com").ToString().ToUpper()

                Case "SEND"
                    If Receive(Page.Request("TXT"), Page.Request("IdUsr"), Page.Request("Random"), Page.Request("MD"), Page.Request("SN"), Page.Request("Part"), Page.Request("FileId"), False) = 0 Then GoTo fail
                Case "RCV"


                    If GetS(Page.Request("IdFrom"), Page.Request("IdUsr"), Page.Request("FileId"), ouinfo, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail
                    Response.Write(Signature.SignatureClass.URLEncode(ouinfo))
                Case "DIR"

                    If Me.Dir(Page.Request("IdUsr"), Page.Request("IdDir"), ouinfo, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail
                    Response.Write(Signature.SignatureClass.URLEncode(ouinfo))

                Case "GU2"

                    If GetU2(Page.Request("IdUsr"), ouinfo, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail
                    Response.Write(Signature.SignatureClass.URLEncode(ouinfo))
                Case "GFRIENDS"
                    If GetIds(Page.Request("IdUsr"), "FriendsIds", ouinfo, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail

                    Response.Write(Signature.SignatureClass.URLEncode(ouinfo))


                Case "LCD"
                    'lood detail
                    Dim bd As String = "", cd As String = "", dd As String = """"

                    If LookUpContactDetails(Page.Request("IdUsr"), Page.Request("Id"), bd, cd, dd, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail
                    Dim rr As String = ""
                    rr = Page.Request("Id") + "/&/" + bd + "/&/" + cd + "/&/" + dd + "/&/"

                    Response.Write(Signature.SignatureClass.URLEncode(rr))
                Case "SRD"
                    'search detail

                    Dim ad As String, bd As String, cd As String, dd As String
                    ad = Page.Request("IdUsr")
                    bd = Page.Request("Name")
                    cd = Page.Request("Surname")
                    dd = Page.Request("Email")
                    If LookDetails(ad, bd, cd, dd, Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail
                    Dim rr As String = ""
                    rr = ad + "/&/" + bd + "/&/" + cd + "/&/" + dd + "/&/"

                    Response.Write(Signature.SignatureClass.URLEncode(rr))
                Case "GD"
                    'getdate
                    Response.Write(GetDate().ToString(Globalization.CultureInfo.InvariantCulture))
                Case "CD"
                    'change details
                    If ChangeEmail(Page.Request("IdUsr"), Page.Request("Email"), Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail


                Case "AF"
                    'add friend
                    If AddId(Page.Request("IdUsr"), Page.Request("Id"), "FriendsIds", Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail

                Case "RF"
                    If Page.Request("IdUsr").Trim.ToUpper = "PUB" Then GoTo fail
                    'remove friend
                    If RemId(Page.Request("IdUsr"), Page.Request("Id"), "FriendsIds", Page.Request("MD"), Page.Request("SN")) = 0 Then GoTo fail




                Case "GS"
                    'get signarture
                    Dim ou As String = ""
                    If Me.GetUnCypheredOnce(Page.Request("IdUsr"), ou) = 0 Then GoTo fail
                    Response.Write(Signature.SignatureClass.URLEncode(ou))
                Case Else
                    GoTo fail

            End Select
            Response.Write("1")
            mts = Nothing
            tryClose()
            Exit Sub
fail:
            Response.Write("0")
            mts = Nothing

            tryClose()


            Exit Sub

        Catch ex As Exception



            Response.Write("-1")



        Finally
            mts = Nothing
            tryClose()
        End Try

    End Sub
    Private Sub tryClose()
        Try
            curcon.Close()

        Catch ex As Exception

        End Try
    End Sub
    Private Function ChangeStatus(ByRef cn As Data.SqlClient.SqlConnection, ByVal Id As String, ByVal newstatus As Integer) As Integer
        If ChangeField(cn, newstatus, Id, "Status") = 0 Then Return 0
        Return 1
    End Function

End Class